package com.bos.controller;

import com.bos.dao.PeriodeDAO;
import com.bos.dao.TahunDAO;
import com.bos.dao.TriwulanDAO;
import com.bos.domain.Periode;
import com.bos.domain.Tahun;
import com.bos.domain.Triwulan;
import com.bos.editor.PeriodeEditor;
import com.bos.editor.TahunEditor;
import com.bos.editor.TriwulanEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/17/11
 * Time: 8:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class PeriodeController {

    private PeriodeDAO periodeDAO;
    private TahunDAO tahunDAO;
    private TriwulanDAO triwulanDAO;

/* --------------------- Getter and Setter ---------------------*/

    @Autowired
    public void setPeriodeDAO(PeriodeDAO periodeDAO) {
        this.periodeDAO = periodeDAO;
    }

    @Autowired
    public void setTahunDAO(TahunDAO tahunDAO) {
        this.tahunDAO = tahunDAO;
    }

    @Autowired
    public void setTriwulanDAO(TriwulanDAO triwulanDAO) {
        this.triwulanDAO = triwulanDAO;
    }

    /* -------------------------- Other Methods -------------------------- */

    @RequestMapping(value = "/periode", method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        modelMap.addAttribute("data", periodeDAO.getAll());

        return "periode/index";
    }

    @RequestMapping(value = "/periode/{id}", method = RequestMethod.GET)
    public String show(ModelMap modelMap, @PathVariable("id") Periode periode) {
        modelMap.addAttribute("data", periode);

        return "periode/show";
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Tahun.class, new TahunEditor(tahunDAO));
        binder.registerCustomEditor(Triwulan.class, new TriwulanEditor(triwulanDAO));
        binder.registerCustomEditor(Periode.class, new PeriodeEditor(periodeDAO));
    }

}
